<template>
	<view class="content">
		<view class="cu-card article">
			<view class="cu-item shadow" v-for="titem in tlist" :key="titem.id">
				<view class="content">
					<image @click="toDetail(titem)" :src="titem.img"
					 mode="aspectFill"></image>
					<view class="desc">
						<view @click="toDetail(titem)" class="text-content">{{titem.name}}</view>
						<view @click="toTel(titem)" class="text-content tel-item" v-if="titem.tel">
							联系电话：{{titem.tel}}
						</view>
						<view  @click="toDetail(titem)">
							<view class="cu-tag bg-red light sm round">{{titem.categoryName}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import { getUserInfo } from "api/user";
	// import { isLogin } from "libs/common";
	import { productInfoList } from 'api/shop'
	import config from 'config'
	// import Map from 'js_sdk/fx-openMap/openMap.js'
	
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				productId:'',
				config:config
			}
		},
		async onLoad(options){
			console.log(options)
			//接收传值,id里面放的是标题，因为测试数据并没写id 
			let categoryId = options.categoryId;
			if(categoryId){
				this.init(categoryId)
			}
			
		},
		methods: {
			toTel(item){
				// let latitude = '116.397428'
				// let longitude = '39.90923'
				// let name = '6666'
				// Map.openMap(latitude, longitude, name)
				window.location.href = 'tel:' + item.tel
			},
			toDetail(item){
				console.log(item)
				uni.redirectTo ({
					url: '/pages/product/product?id='+item.id
				});	
			},
			init(categoryId){
				productInfoList ({categoryId:categoryId,brandId:config.brandId}).then (ret => {
					this.tlist = ret.data.data
				})
			},
			async loadData(){
				let list = await this.$api.json('cateList');
				list.forEach(item=>{
					if(!item.pid){
						this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
					}else if(!item.picture){
						this.slist.push(item); //没有图的是2级分类
					}else{
						this.tlist.push(item); //3级分类
					}
				}) 
			},
			//一级分类点击
			tabtap(item){
				console.log(item,'类型信息')
				
				
				
				if(!this.sizeCalcState){
					this.calcSize();
				}
				
				this.currentId = item.id;
				let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;
		&.active{
			color: $base-color;
			background: #f8f8f8;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12upx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;
		
		image{
			width: 140upx;
			height: 140upx;
		}
	}
	.cu-card>.cu-item{
		margin: 0;
	}
	body.pages-category-category uni-page-body, .content{
		background: #FFFFFF;
	}
	.cu-card.article>.cu-item .content>uni-image{
		border: 1px solid #ddd;
	}
	.cu-card{
		overflow: inherit;
		width: 100%;
	}
	.cu-card.article>.cu-item .content .text-content{
		height: initial;
		margin-top: 5%;
	}
	.tel-item{
		font-size: 13px !important;
		margin-top: 0 !important;
	}
</style>
